package com.kzd.gtgame.main.access

import androidx.core.widget.addTextChangedListener
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.jetpack.vm.AccessViewModel
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import kotlinx.android.synthetic.main.fragment_access_pwd_forget.*
import java.util.concurrent.TimeUnit

class AccessPwdForgetFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessPwdForgetFragment {
            return AccessPwdForgetFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_pwd_forget

    override fun initView() {
        etAccessPwdPhone.addTextChangedListener {
            tvAccessPwdSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessPwdCode.text.isNullOrEmpty() &&
                    !etAccessPwdNew.text.isNullOrEmpty()
            tvAccessPwdGetCode.isEnabled = !it.isNullOrEmpty() && !isCodeDownTime
        }
        etAccessPwdCode.addTextChangedListener {
            tvAccessPwdSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessPwdPhone.text.isNullOrEmpty() &&
                    !etAccessPwdNew.text.isNullOrEmpty()
        }
        etAccessPwdNew.addTextChangedListener {
            tvAccessPwdSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !etAccessPwdPhone.text.isNullOrEmpty() &&
                    !etAccessPwdCode.text.isNullOrEmpty()
        }

        tvAccessPwdGetCode.setOnClickAntiShakeListener {
            viewModel.accessGetCode(etAccessPwdPhone.text.toString())
        }

        tvAccessPwdSubmit.setOnClickAntiShakeListener {

            viewModel.accessForgetPwd(
                etAccessPwdPhone.text.toString(),
                etAccessPwdNew.text.toString(),
                etAccessPwdCode.text.toString()
            )
        }
    }

    override fun initViewMode() {
        viewModel.accessGetCodeLiveData.observe(viewLifecycleOwner, { response ->

//            if (response.status == Status.START) {
//                startCodeDownTime()
//            }

            if (response.status != Status.SUCCESS)
                return@observe

            startCodeDownTime()
        })

        viewModel.accessForgetPwdLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            toast(response.data?.message)
            activity?.onBackPressed()
        })
    }


    private var isCodeDownTime = false

    /**
     * 获取验证码倒计时
     * */
    fun startCodeDownTime() {
        Flowable.intervalRange((1), (60), (0), (1), TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext {
                tvAccessPwdGetCode.text = "剩${60 - it}秒"
            }
            .doOnSubscribe {
                isCodeDownTime = true
                tvAccessPwdGetCode.isEnabled = false
            }
            .doOnComplete {
                isCodeDownTime = false
                tvAccessPwdGetCode.text = "发送验证码"
                tvAccessPwdGetCode.isEnabled =
                    !etAccessPwdPhone.text.isNullOrEmpty()
            }
            .subscribe()
    }

}